"use strict";
const path = require('path');
const fs = require('fs');
const {getConfig} = require("./utils");

// 配置运行指令
async function addInstructions() {
    // const packagePath = path.join(process.cwd(), 'package.json');
    let packagePath =  "".concat(process.env.INIT_CWD, "/package.json")
    // 检查文件路径
    if (!path.isAbsolute(packagePath)) {
        console.error('路径必须是绝对路径');
        return;
    }

    let content = {
        scripts: {},
    };

    try {
        // 读取 package.json 文件
        const fileContent = fs.readFileSync(packagePath, 'utf-8');
        content = JSON.parse(fileContent);
        // 更新 scripts
        content.scripts = Object.assign(Object.assign({}, content.scripts), { translate: "node node_modules/vue-translate-auto" });
        // 写入 package.json 文件
        fs.writeFileSync(packagePath, JSON.stringify(content, null, 2), 'utf-8');
        // console.log('指令 translate 添加成功');
    } catch (err) {
        console.error(`添加指令 translate 失败: ${err.message},可手动添加运行指令 translate: "node node_modules/vue-translate-auto"`);
    }
}

// 创建配置文件
var addConfig = function () {
    // let targetPath = path.join(process.cwd(), 'vue-translate-config.json');
    let targetPath =  "".concat(process.env.INIT_CWD, "/vue-translate-config.json")
    // 检查目标文件是否存在
    if (!fs.existsSync(targetPath)) {
        // 写入目标文件
        fs.writeFileSync(targetPath, JSON.stringify(getConfig(), null, 2), 'utf8');
        // console.log('vue-translate-config.json 已创建');
    } else {
        // console.log('vue-translate-config.json 已存在，跳过创建');
    }
};

(async function () {
    addConfig();
    await addInstructions();
})();